/**
 * 用户体验优化相关的API服务
 * 封装后端API调用，避免直接操作api目录
 */

import request from '@/request'

/**
 * 中断AI生成任务
 * @param appId 应用ID (字符串类型，避免精度丢失)
 */
export async function interruptGenerationTask(appId: string | number) {
  return request<{ code: number; data: boolean; message: string }>(`/app/interrupt/${appId}`, {
    method: 'POST',
  })
}

/**
 * 下线已部署的应用
 * @param appId 应用ID (字符串类型，避免精度丢失)
 */
export async function undeployApplication(appId: string | number) {
  return request<{ code: number; data: boolean; message: string }>(`/app/undeploy/${appId}`, {
    method: 'POST',
  })
}

/**
 * 获取应用当前生成状态
 * @param appId 应用ID (字符串类型，避免精度丢失)
 * @returns 状态字符串：NOT_GENERATED | GENERATING | COMPLETED | FAILED | INTERRUPTED
 */
export async function getApplicationStatus(appId: string | number) {
  return request<{ code: number; data: string; message: string }>(`/app/status/${appId}`, {
    method: 'GET',
  })
}

/**
 * 状态字符串转换为数字枚举
 * @param status 状态字符串
 */
export function convertStatusStringToNumber(status: string): number {
  const statusMap: { [key: string]: number } = {
    'NOT_GENERATED': 0,
    'GENERATING': 1,
    'COMPLETED': 2,
    'FAILED': 3,
    'INTERRUPTED': 4,
  }
  return statusMap[status] || 0
}
